package com.example.backend.tools;

import com.example.backend.mapper.FoodMapper;
import com.example.backend.mapper.RestaurantMapper;
import com.example.backend.pojo.Foodorder;
import com.example.backend.pojo.Orderdetail;
import com.example.backend.pojo.Restaurant;
import com.example.backend.pojo.VO.OrderdetailVO;
import com.example.backend.service.OrderService;
import dev.langchain4j.agent.tool.P;
import dev.langchain4j.agent.tool.Tool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class RestaurantTools {
    @Autowired
    private OrderService orderService;
    @Autowired
    private FoodMapper foodMapper;
    @Autowired
    private RestaurantMapper restaurantMapper;


    @Tool(name = "placeOrder",  value = "使用用户提供的信息来下单")
    String placeOrder(
        @P(value = "人数") Integer person,
        @P(value = "订单详情") List<OrderdetailVO> orderDetails,
        @P(value = "总价格") Double price,
        @P(value = "餐厅名称") String restaurantName
    ) {
        System.out.println("下单信息："+"人数："+person+"订单详情："+orderDetails+"价格："+price+"餐厅名称："+restaurantName);

//        创建FoodOrder对象

        Foodorder foodorder = new Foodorder();
        foodorder.setPerson(person);
        foodorder.setWay(1);
        foodorder.setPrice(price);

//        获取餐厅id
        foodorder.setRestaurantid(restaurantMapper.getRestaurantIdByName(restaurantName));
//        List 转换
        List<Orderdetail>  orders = orderDetails.stream().map(orderdetailVO -> {
            Orderdetail orderdetail = new Orderdetail();
            orderdetail.setFoodid(foodMapper.getfoodByFoodName(orderdetailVO.getFoodname()));
            orderdetail.setPrice(orderdetailVO.getPrice());
            orderdetail.setNumber(orderdetailVO.getNumber());
            return orderdetail;
        }).toList();
        foodorder.setOrders(orders);

        orderService.addOrder(foodorder);
        Restaurant r = restaurantMapper.getRestaurantById(foodorder.getRestaurantid());
        restaurantMapper.surplus(r.getRestaurantid(), r.getSurplus()-1);
        restaurantMapper.updatesales(r.getSales()+1,r.getRestaurantid());

        return "下单成功";
    }

}
